<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use app\common\controller\Email;

/**
 * 订单列表管理
 * Class User
 * @package app\worker\controller
 */
class Order extends Controller
{
    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'Order';

    /**
     * 订单列表管理
     * @auth true
     * @menu true
     */
    public function index()
    {
        //获取仓库表
        $where = [
            ['is_deleted', '=', 0],
            ['status', '=', 1],
        ];
        if(session('user.ware_house_id')){
            $where[] = ['id', '=', session('user.ware_house_id')];
         }
        $this->ware_house = Db::name('WareHouse')->where($where)->order('sort desc')->select();
        array_unshift($this->ware_house, ['id'=>0, 'name'=>'请选择仓库']);

        $this->title = '订单列表管理';

        $query = $this->_query($this->table)
            ->alias('o')
            ->join('user u', 'u.id = o.uid', 'left')
            ->join('order_detail od', 'od.order_id = o.id', 'left')
            ->join('plan_detail pd', 'pd.id = od.plan_detail_id', 'left')
            ->join('plan p', 'p.id = pd.plan_id', 'left')
            ->join('body b', 'b.id = u.body_id', 'left')
            ->dateBetween('p.plan_date#plan_date')
            ->where(['o.is_deleted' => '0']);
        if(session('user.ware_house_id')){
            $query->where('p.ware_house_id', session('user.ware_house_id'));
        }
        if(request()->get('ware_house_id')){
            $query->where('p.ware_house_id', request()->get('ware_house_id'));
        }
        $query->like('u.name#user_name')
            ->field('o.*,
            sum(od.complete_count) as sum,sum(od.price*od.complete_count) as sum_price,sum(od.timeout_fine_price+od.goods_fine_price+od.other_fine_price) as fine_price,
            b.body_name,u.name as user_name,
            u.phone,u.ID_number,u.bank_number,u.type,u.att,u.pay_way,u.payment_way,u.car_number,u.car_type,u.in_time,u.out_time,u.classify')
            ->order('o.create_at desc')
            ->group('o.id')
            ->page();
    }

    protected function _index_page_filter(&$data) {
        foreach($data as &$v){
            $v['sum'] = $v['sum']; //统计完成件数
            $v['sum_price'] =$v['sum_price'];//计算总金额
            $v['fine_price'] =$v['fine_price'];//扣罚金额
            $v['reality_price'] = $v['sum_price'] - $v['fine_price'];//应发金额
            $v['complete_pay_price'] = Db::name('pay_log')->where('table', 'user')->where('primary_key', $v['uid'])->sum('price');//已支付金额
            $v['wait_pay_price'] = $v['sum_price'] - $v['complete_pay_price'];//待支付金额
        }
    }

    /**
     * 添加订单信息
     * @auth false
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 修改订单信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        //成员信息列表
        $where = [
            ['is_deleted', '=', 0],
            ['status', '=', 1],
        ];
        //过滤离职时间大于等于当前时间的数据
        $where[] = [
            ['out_time', ['>=', date('Y-m-d')], ['=', ''], 'or'],
        ];
        if(session('user.ware_house_id')){
            $where[] = ['ware_house_id', '=', session('user.ware_house_id')];
         }
        $this->user = Db::name('User')->where($where)->order('create_at desc')->select();
        foreach($this->user as &$v){
            if ($v['type'] == 0) {
                $type = '运力';
            } elseif ($v['type'] == 1) {
                $type = '分拣';
            }
            if ($v['classify'] == 0) {
                $classify = '固定';
            } elseif ($v['classify'] == 1) {
                $classify = '临时';
            }
            if ($v['att'] == 0) {
                $att = '车队';
            } elseif ($v['att'] == 1) {
                $att = '个人';
            } elseif ($v['att'] == 2) {
                $att = '劳务';
            } elseif ($v['att'] == 3) {
                $att = '自招';
            }
            if ($v['payment_way'] == 0) {
                $payment_way = '周结';
            } elseif ($v['payment_way'] == 1) {
                $payment_way = '月结';
            } elseif ($v['payment_way'] == 2) {
                $payment_way = '无';
            }
            $v['name'] = $v['name'] . '：' . $type . '-' . $classify . '-' . $att . '-' . $payment_way;
        }
        if(request()->isPost()){
            if(!isset($data['id']) && Db::name('Order')->where('uid', $data['uid'])->where('is_deleted', 0)->find()){
                $this->error('该作业人员订单已存在，请勿重复添加');
            }
            $data['other_fine_price'] = $data['other_fine_price'] ? $data['other_fine_price'] : 0;
            $data['goods_fine_price'] = $data['goods_fine_price'] ? $data['goods_fine_price'] : 0;
            $data['timeout_fine_count'] = $data['timeout_fine_count'] ? $data['timeout_fine_count'] : 0;
            $data['timeout_fine_price'] = $data['timeout_fine_price'] ? $data['timeout_fine_price'] : 0;
            //扣罚金额
            $data['fine_price'] = $data['other_fine_price'] + $data['goods_fine_price'] + $data['timeout_fine_count'] * $data['timeout_fine_price'];
            
        }
    }

    /**
     * 删除订单信息
     * @auth false
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    /**
     * 查看订单流水
     * @auth true
     */
    public function order_detail_index(){

    }
    /**
     * 更改抽检状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function is_check()
    {
        $this->_save($this->table, ['is_check' => input('is_check', '0')]);
    }

    //获取运营计划列表
    public function getPlans()
    {
        $ware_house_id = request()->get('id');
        $plans = Db::name('Plan')
            ->where('is_deleted', 0)
            ->where('status', 1)
            ->where('ware_house_id', $ware_house_id)
            ->order('plan_date desc, create_at desc')
            ->select();
        $this->success('获取成功', $plans);
    }

    //获取运营计划明细列表
    public function getPlanDetails()
    {
        $plan_id = request()->get('id');
        $plan_details = Db::name('PlanDetail')
            ->where('plan_id', $plan_id)
            ->order('type asc, classify asc')
            ->select();
        foreach ($plan_details as &$v) {
            if ($v['type'] == 0) {
                $type = '运力';
            } elseif ($v['type'] == 1) {
                $type = '分拣';
            }
            if ($v['classify'] == 0) {
                $classify = '固定';
            } elseif ($v['classify'] == 1) {
                $classify = '临时';
            }
            if ($v['att'] == 0) {
                $att = '车队';
            } elseif ($v['att'] == 1) {
                $att = '个人';
            } elseif ($v['att'] == 2) {
                $att = '劳务';
            } elseif ($v['att'] == 3) {
                $att = '自招';
            }
            if ($v['payment_way'] == 0) {
                $payment_way = '周结';
            } elseif ($v['payment_way'] == 1) {
                $payment_way = '月结';
            } elseif ($v['payment_way'] == 2) {
                $payment_way = '无';
            }
            if ($v['jobs'] == 0) {
                $jobs = '有车';
            } elseif ($v['jobs'] == 1) {
                $jobs = '无车';
            } elseif ($v['jobs'] == 2) {
                $jobs = '纯分拣';
            } elseif ($v['jobs'] == 3) {
                $jobs = '冻品';
            } elseif ($v['jobs'] == 4) {
                $jobs = '大件';
            }
            $v['name'] = $type . '-' . $classify . '-' . $att . '-' . $payment_way . '-' . $jobs . '-单价:' . $v['price'];
        }
        $this->success('获取成功', $plan_details);
    }
    //获取运营计划明细信息
    public function getPlanDetail()
    {
        $id = request()->get('id');
        $plan_detail = Db::name('PlanDetail')->where('id', $id)->find();
        $this->success('获取成功', $plan_detail);
    }
}
